package com.hkrda523bpwp.hkr_da523b_projekt_pwp;


import com.google.android.youtube.player.YouTubeIntents;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

	
	ActionBar actionBar;
	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPagerNoSwipe mViewPager;
	Intent intent;
	LocationHandler locationHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager(), getApplicationContext());

		mViewPager = (ViewPagerNoSwipe) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		mViewPager
				.setOnPageChangeListener(new ViewPagerNoSwipe.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						actionBar.setSelectedNavigationItem(position);
					}
				});

		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
		
		locationHandler = new LocationHandler(getApplicationContext(), mViewPager);
	}
	
	public void onClickSendbookingReq(View view){
		
		EditText et = (EditText) mViewPager.getRootView().findViewById(R.id.booking_editText_1);
		RadioGroup rg = (RadioGroup) mViewPager.getRootView().findViewById(R.id.booking_radioGroup1);
		RadioButton rb = (RadioButton) mViewPager.getRootView().findViewById(rg.getCheckedRadioButtonId());
		CheckBox cb = (CheckBox) mViewPager.getRootView().findViewById(R.id.booking_checkBox_KE);
		
		if (rg.getCheckedRadioButtonId() != -1){
		
			String to = "info@patricwilhelmsson.se";
			String subject = "Bokningsförfrågan";
			String meddelande = "";
			
			meddelande += rb.getText().toString()+"\n\n";
			
			if (cb.isChecked()){
				meddelande += "-------------\n\n";
				meddelande += "Longitude: "+locationHandler.getLongitude()+"\n";
				meddelande += "Latitude: "+locationHandler.getLatitude()+"\n\n";
			}
			
			meddelande += "-------------\n\n";
			meddelande += et.getText().toString();
			
			Intent email = new Intent(Intent.ACTION_SEND);
			email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
			email.putExtra(Intent.EXTRA_SUBJECT, subject);
			email.putExtra(Intent.EXTRA_TEXT, meddelande);
	
			email.setType("message/rfc822");
	
			startActivity(Intent.createChooser(email, "Välj e-post klient:"));
		}
	}

	public void onClickImageView(View view){
		boolean flag = false;
		String vid_id = "";
		switch (Integer.parseInt(view.getTag().toString())) {
		case 1:
			vid_id="8prtOofSYCo";
			break;
		case 2:
			vid_id="CID6Fl4PHic";
			break;
		case 4:
			vid_id="IchC97jT2e4";
			break;
		case 5:
			vid_id="IChe3SkDv4M";
			break;
		case 6:
			vid_id="lj0L7cR1QeY";
			break;
		case 7:
			vid_id="lu489QvQJrk";
			break;
		case 8:
			vid_id="p3CqQh87pSA";
			break;
		default:
			flag = true;
			break;
		}
		if (!flag){
			intent = YouTubeIntents.createPlayVideoIntentWithOptions(this, vid_id, true, true);
			startActivity(intent);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK && actionBar.getSelectedTab().getPosition() != 0){
			mViewPager.setCurrentItem(0, false);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void hideSoftKeyboard(){		
		View view = mViewPager.getRootView().findFocus();
		
		if (view != null) {
		    InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
        case R.id.action_exit:
        	finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		mViewPager.setCurrentItem(tab.getPosition(), false);
		
		if (tab.getPosition() == 2){
			locationHandler.setABT(tab);
			locationHandler.start();
		}
		
		hideSoftKeyboard();
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	

}
